package com.cheer.controller;

import com.cheer.common.Page;
import com.cheer.common.Result;
import com.cheer.dto.OaArticleClassAddDTO;
import com.cheer.dto.OaArticleClassUpdateDTO;
import com.cheer.service.OaArticleClassService;
import com.cheer.vo.OaArticleClassInfoVO;
import com.cheer.vo.OaArticleClassListVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/article/class")
public class OaArticleClassController {

    @Autowired
    private OaArticleClassService oaArticleClassService;

    @ApiOperation("添加文章分类")
    @PostMapping("/add")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result add(@RequestBody @Valid OaArticleClassAddDTO articleClassAddDTO) {
        oaArticleClassService.add(articleClassAddDTO);
        return Result.success();
    }

    @ApiOperation("查询文章分类列表")
    @GetMapping("/list")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<Page<OaArticleClassListVO>> list(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                   @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return Result.success(oaArticleClassService.list(pageNum, pageSize));
    }

    @ApiOperation("查询文章分类详情")
    @GetMapping("/info")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<OaArticleClassInfoVO> info(@RequestParam Integer id) {
        return Result.success(oaArticleClassService.info(id));
    }

    @ApiOperation("修改文章分类")
    @PostMapping("/update")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result update(@RequestBody @Valid OaArticleClassUpdateDTO articleClassUpdateDTO) {
        oaArticleClassService.update(articleClassUpdateDTO);
        return Result.success();
    }

    @ApiOperation("启用或禁用")
    @GetMapping("/status")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result updateStatus(@RequestParam Integer id, @RequestParam Integer status) {
        oaArticleClassService.updateStatus(id, status);
        return Result.success();
    }

}
